package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

public class Session {

    private BufferedReader reader;
    private Map<String, User> userList;

    public Session(BufferedReader reader) {
        initUserList();
        this.reader = reader;
    }

    private void initUserList() {
        userList = new TreeMap<String, User>();
        userList.put("111-2222", new User("name1", "email1", "phone1", "password1"));
        userList.put("222-3333", new User("name2", "email2", "phone3", "password4"));
        userList.put("333-4444", new User("name3", "email3", "phone3", "password3"));
    }

    public  Map<String, User> getAllUsers() {
        return userList;
    }

    public boolean login() throws IOException {
        System.out.println("please input your account number: ");
        String account = reader.readLine();
        if (!userList.keySet().contains(account)){
            System.out.println("no such user\n");
            return false;
        }
        if (userList.keySet().contains(account)) {
            System.out.println("please input your password: ");
            String password = reader.readLine();
            if (password.equalsIgnoreCase(userList.get(account).password)) {
                System.out.println("log in successfully!\n");
                System.out.println(userInfo(account));
                return true;
            }
        }
        System.out.println("password is wrong. login failed!\n");
        return false;
    }

    public String userInfo(String account) {
        String result = "The current user is \n";
        User user = userList.get(account);
        result += user.name + "---" + user.email + "---" + user.phone_number + "\n";
        return result;
    }
}
